package cn.rengy.web.framework.wxmp.wxdialogue;

import java.util.List;
import java.util.Map;

import jakarta.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import cn.rengy.web.framework.sql.DbService;

@Service
public class DialogueService {
	private static Logger logger = LoggerFactory.getLogger(DialogueService.class);
	
	@Autowired
	private CacheManager cacheManager;
	@Autowired
	private DbService dbService;
	/**
	 * 开启对话，不保存数据库
	 * @param dialogue
	 */
	public void openMemoryDialogue(Map<String,Object> dialogue){
		Object openid=dialogue.get("openid");
		Cache cache=cacheManager.getCache("dialogueCache");
		//Map<String,Object> dialogue=new HashMap<String,Object>();
		cache.put(openid, dialogue);
	}
	/**
	 * 开启对话
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void openDialogue(Map<String,Object> dialogue){
		Object openid=dialogue.get("openid");
		Object start_time= dialogue.get("start_time");
		Object dialogueType=dialogue.get("type");
		// TODO
		List<Map<String,Object>> bindProduct=null;
		//List<Map<String,Object>> bindProduct=(List<Map<String,Object>>)dialogue.get("dialogue_expectation");
		//Assert.notNull(bindProduct, "bindProduct must not be null");
		long returnid=this.dbService.updateReturnGeneratedKeys("insert_lz_wx_dialogue", dialogue).longValue();
		dialogue.put("id", returnid);
		//保存期待值，不需要？
		/*for(int i=0;i<bindProduct.size();i++){
			Map<String,Object> product=bindProduct.get(i);
			Map<String,Object> expectation=new HashMap<String,Object>();
			expectation.put("dialogue_id", returnid);
			//pins.id product_instance_id,prod.product_name,plan.plan_name
			//(dialogue_id,expectation_value,mapping_value,order_no)
			expectation.put("expectation_value", i+1);
			expectation.put("mapping_value", product.get("product_instance_id"));
			this.dbService.update("insert_lz_wx_dialogue_expectation", expectation);
		}*/
		Cache cache=cacheManager.getCache("dialogueCache");
		//Map<String,Object> dialogue=new HashMap<String,Object>();
		cache.put(openid, dialogue);
	}
	/**
	 * 关闭对话
	 * @param openid
	 */
	public void closeDialogue(String openid){
		Cache cache=cacheManager.getCache("dialogueCache");
		cache.evict(openid);
	}
	/**
	 * 查询对话
	 * @param openid
	 * @return
	 */
	public Map<String,Object> getDialogue(String openid){
		Cache cache=cacheManager.getCache("dialogueCache");
		ValueWrapper valueWrapper=cache.get(openid);
		if(valueWrapper==null){
			return null;
		}else{
			//TODO
			return null;
			//return (Map<String,Object>)valueWrapper.get();
		}
	}
	/**
	 * 更新维持缓存
	 * @param dialogue
	 */
	public void updateDialogue(Map<String,Object> dialogue){
		Object openid=dialogue.get("openid");
		Cache cache=cacheManager.getCache("dialogueCache");
		//Map<String,Object> dialogue=new HashMap<String,Object>();
		cache.put(openid, dialogue);
	}
}
